<?php

if_get('/engine_logs', function ()
{/*{{{*/
    list(
        $inputs['engine_rule_id']
    ) = input_list(
        'engine_rule_id'
    );

    $inputs = array_filter($inputs, 'not_null');

    $engine_logs = dao('engine_log')->find_all_by_column($inputs);

    return [
        'count' => count($engine_logs),
        'engine_logs' => array_build($engine_logs, function ($id, $engine_log) {
            return [
                null,
                [
                    'id' => $engine_log->id,
                    'engine_rule_display' => $engine_log->engine_rule->display_for_engine_logs_engine_rule(),
                    'create_time' => $engine_log->create_time,
                    'update_time' => $engine_log->update_time,
                ]
            ];
        }),
    ];
});/*}}}*/

if_post('/engine_logs/add', function ()
{/*{{{*/

    $engine_log = engine_log::create();

    $engine_log->engine_rule = dao('engine_rule')->find(input('engine_rule_id'));

    return [
        'id' => $engine_log->id,
    ];
});/*}}}*/

//todo::detail

if_post('/engine_logs/update/*', function ($engine_log_id)
{/*{{{*/

    $engine_log = dao('engine_log')->find($engine_log_id);
    otherwise_error_code('ENGINE_LOG_NOT_FOUND', $engine_log->is_not_null());

    $engine_log->engine_rule = dao('engine_rule')->find(input('engine_rule_id'));

    return true;
});/*}}}*/

if_post('/engine_logs/delete/*', function ($engine_log_id)
{/*{{{*/
    $engine_log = dao('engine_log')->find($engine_log_id);
    otherwise_error_code('ENGINE_LOG_NOT_FOUND', $engine_log->is_not_null());

    $engine_log->delete();

    return true;
});/*}}}*/
